home *** CD-ROM | disk | FTP | other *** search
Wrap
;;;; ;;;; PURPOSE: color palette (demo for drag&drop facilities) ;;;; ;;;; This file was originally written in Tcl for the BLT package by ;;;; Michael J. McLennan Phone: (215)770-2842 ;;;; AT&T Bell Laboratories E-mail: aluxpo!mmc@att.com ;;;; Copyright (c) 1993 AT&T All Rights Reserved ;;;; ;;;; ;;;; Rewritten for STklos by Erick Gallesio ;;;; Creation date: 6-Jul-1994 09:53 ;;;; Last file update: 28-Dec-1995 19:05 (require "blt") (require "dd-protocol.stklos") (require "Scale") (require "Message") (require "Lentry") (define (hexa n) (string-append (number->string (quotient n 16) 16) (number->string (modulo n 16) 16))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; ;;;; Routines for packaging token windows... ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (package-color color win) (let* ((rgb (winfo 'rgb *root* color)) (r (quotient (car rgb) 256)) (g (quotient (cadr rgb) 256)) (b (quotient (caddr rgb) 256))) (make-drag&drop-label win :text "Color" :background color :foreground (if (> (+ r g b) 384) "black" "white")) color)) (define (set-colors) (let ((rgb (winfo 'rgb *root* (hash-table-get DragDrop 'color "black")))) (set! (value Red) (quotient (car rgb) 256)) (set! (value Green) (quotient (cadr rgb) 256)) (set! (value Blue) (quotient (caddr rgb) 256)))) (define (package-number num win) (make-drag&drop-label win :text (format #f "Number: ~A" num)) num) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; ;;;; A Class for color Slides ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-class <Color-Scale>(<Tk-composite-widget> <Scale>) ((sample :accessor sample) (scale :accessor scale-of) (background :accessor background :init-keyword :background :allocation :special :propagate (frame scale)) (foreground :accessor foreground :init-keyword :foreground :allocation :special :propagate (frame scale)) (format :accessor format-of :init-keyword :format))) ;; "#~A0000", "#00~A00" or "#0000~A" (define-method initialize-composite-widget ((self <Color-Scale>) args parent) (let ((s (make <Scale> :parent parent :from 0 :to 255 :command (lambda (v) (set! (value self) v)) :orientation "horizontal")) (f (make <Frame> :parent parent :width 20 :height 20 :border-width 3 :relief "raised"))) ;; Manage components (slot-set! parent 'border-width 3) (slot-set! parent 'relief "groove") (pack s :side "left" :expand #t :fill 'x) (pack f :side "right" :fill 'y) ;; Assign slots (slot-set! self 'Id (Id s)) (slot-set! self 'sample f) (slot-set! self 'scale s) ;; Drag & Drop (drag&drop-configure f :package-command (lambda (v) (package-color (background (sample self)) v)) :source-handler `(color ,dd-send-color)) (drag&drop-configure s :package-command (lambda (v) (package-number (value self) v)) :source-handler `(number ,dd-send-number) :target-handler `(number ,(lambda () (set! (value self) (hash-table-get DragDrop 'number))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; ;;;; procedure to change (fg or bg) color of a window and its descendants ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (change-color widgets foreground) (let* ((rgb (winfo 'rgb *root* (hash-table-get DragDrop 'color ""))) (newR (quotient (car rgb) 256)) (newG (quotient (cadr rgb) 256)) (newB (quotient (caddr rgb) 256)) (actR (- newR 20)) (actG (- newG 20)) (actB (- newB 20)) (ncolor (string-append "#" (hexa newR) (hexa newG) (hexa newB))) (acolor (string-append "#" (hexa actR) (hexa actG) (hexa actB)))) (let ((change (lambda (win) (if foreground (catch (set! (foreground win) ncolor) (set! (active-foreground win) acolor)) (catch (set! (background win) ncolor) (set! (active-background win) acolor)))))) (for-each (lambda (x) (change x)) widgets)))) ;;;; ---------------------------------------------------------------------- ;;;; Main application window... ;;;; ---------------------------------------------------------------------- ;;;; main-sample (define main-sample (make <Label> :text "Color" :border-width 3 :relief "raised")) (drag&drop-configure main-sample :package-command (lambda (w) (package-color (format #f "#~A~A~A" (hexa (value Red)) (hexa (value Green)) (hexa (value Blue))) w)) :source-handler `(color ,dd-send-color) :target-handler `(color set-colors)) ;;;; explanation (define explanation (make <Message> :font "-Adobe-times-medium-r-normal--*-120*" :aspect 200 :text "Press the third mouse button over a slider or a color sample and drag the token window around. When the token becomes raised, it is over a target window. Release the mouse button to drop the token and transfer information. If the transfer fails, a \"no\" symbol is drawn on the token window. Try the following: - Drop a number from one slider onto another - Drop a color sample onto the Foreground/Background targets - Drop one of the slider color samples onto the main sample - Drop tokens from one palette application onto another")) ;;;; entry (define ent (make <Labeled-Entry> :title "Color Value:" :border-width 2 :relief "sunken")) (drag&drop-configure ent :package-command (lambda (w) (package-color (value ent) w)) :source-handler `(color ,dd-send-color) :target-handler '(color ,(lambda () (set! (value ent) (hash-table-get DragDrop 'color))))) (bind ent "<Key-Return>" '(hash-table-put! DragDrop 'color (value ent))) ;;;; Red/Green/Blue (define Red (make <Color-Scale> :text "Red" :format "#~A0000")) (define Green (make <Color-Scale> :text "Green" :format "#00~A00")) (define Blue (make <Color-Scale> :text "Blue" :format "#0000~A")) ;;; ;;; Overload the (setter value) of <Color-Scale> so that modification of a slider ;;; is reported to the main sample ;;; (define-method (setter value) ((s <Color-Scale>) v) (set! (background (sample s)) (format #f (format-of s) (hexa v))) (set! (value (scale-of s)) v) (let ((r (value Red)) (g (value Green)) (b (value Blue))) ;; Update main sample (set! (background main-sample) (format #f "#~A~A~A" (hexa r) (hexa g) (hexa b))) (set! (foreground main-sample) (if (> (+ r g b) 384) "black" "white")))) ;;;; ;;;; Foreground/Background color inputs... ;;;; (define inputs (make <Frame>)) (define bg (make <Label> :text "Background" :parent inputs :border-width 3 :relief 'groove)) (define fg (make <Label> :text "Foreground" :parent inputs :border-width 3 :relief 'groove)) (drag&drop-configure bg :target-handler `(color ,(lambda () (change-color *the-widgets* #f)))) (drag&drop-configure fg :target-handler `(color ,(lambda () (change-color *the-widgets* #t)))) ;;;; Quit (define quit (make <Button> :text "Quit" :parent inputs :border-width 3 :command "exit")) ;;;; ;;;; Pack all the widgets ;;;; (pack bg fg :side "left" :padx 5 :pady 5) (pack quit :side "right" :padx 5 :pady 5) (pack main-sample explanation ent :expand #t :fill "both") (pack Red Green Blue :fill "both") (pack inputs :fill "x") (wm 'minsize *root* 200 200) (wm 'maxsize *root* 1000 1000) ;; List of widgets whose bg/fg is changed when a global change-color is done (define *the-widgets* (list explanation ent Red Green Blue inputs bg fg quit)) (set-colors)